<?php
/**
 * UserGroupFunctionController
 *
 * PHP Version 5.4
 *
 * @author liuxuTJ <liuxuphp@icesmart.cn>
 */
namespace Admin\Controller;
use Admin\Service\PermissionService;
use Admin\Service\UserService;
use Model\UserGroupFunctionModel;
use Think\Controller;

/**
 * Class UserGroupFunctionController
 */
class UserGroupFunctionController extends BaseController {

    public function __construct()
    {
        parent::__construct();
        $userService = new UserService();
        $userInfo = $userService->validateLogin();   //验证是否登录

        $permissionService = new PermissionService();
        $permission = $permissionService->validate($userInfo['userId'], [
            'add' => '/'.CONTROLLER_NAME.'/add',
            'del' => '/'.CONTROLLER_NAME.'/del',
            'edit' => '/'.CONTROLLER_NAME.'/edit'
        ]);

        $menu = $permissionService->menu($userInfo['userId']);

        $this->assign('menu', $menu);
        $this->assign('permission', $permission);
        $this->assign('userInfo', $userInfo);
    }

    /**
     * 权限列表 查询
     */
    public function index()
    {
        $userGroupFunctionModel = new UserGroupFunctionModel();
        $page = $this->page($userGroupFunctionModel, null);
        $show = $page->show();
        $functions = $userGroupFunctionModel->order('createTime DESC')->select();
        foreach($functions as $k=>$v){
            $functions[$k]['parentInfo'] = $userGroupFunctionModel->field('name')->get($v['categoryId']);
        }
        $functions = $userGroupFunctionModel->structure($functions);

        $groupFunctionList = $userGroupFunctionModel->where('categoryId = 0')->order('createTime DESC')->select();

        $this->assign('url', '/UserGroupFunction/index');
        $this->assign('groupFunctions', $groupFunctionList);
        $this->assign('functions', $functions);
        $this->assign('page',$show);
        $this->display();
    }

    /**
     * 权限 添加
     */
    public function add()
    {
        $name = I('post.name');
        $categoryId = I('post.categoryId');
        $url = I('post.url');
        $isMenu = I('post.isMenu');
        $sort = I('post.sort');
        $createTime = date('Y-m-d H:i:s');

        if(empty($name) || empty($url)){
            $this->error('带*处为必填项');
        }

        $userGroupFunctionModel = new UserGroupFunctionModel();
        $userGroupFunctionAdd = $userGroupFunctionModel->add([
            'name' => $name,
            'categoryId' => $categoryId,
            'url' => $url,
            'isMenu' => $isMenu,
            'sort' => $sort,
            'createTime' => $createTime
        ]);

        if($userGroupFunctionAdd){
            $this->success('添加成功', '/Admin/UserGroupFunction/',1);
        }else{
            $this->error('添加失败');
        }
    }

    /**
     * 权限 删除
     */
    public function del()
    {
        $functionId = I('get.id');
        if(empty($functionId)){
            $this->error('参数异常');
        }

        $userGroupFunctionModel = new UserGroupFunctionModel();
        $functionDel = $userGroupFunctionModel->where('id = '.$functionId)->delete();

        if($functionDel){
            $this->success('删除成功', '/Admin/UserGroupFunction/');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * 权限 修改
     */
    public function edit()
    {
        $functionId = I('get.id');
        if(empty($functionId)){
            $this->error('参数异常');
        }

        $userGroupFunctionModel = new UserGroupFunctionModel();
        $function = $userGroupFunctionModel->get($functionId);

        $functionCategories = $userGroupFunctionModel->findByFilter(['categoryId' => 0],['createTime' => 'DESC']);

        $this->assign('url', '/UserGroupFunction/index');
        $this->assign('function', $function);
        $this->assign('functionCategories', $functionCategories);
        $this->display();
    }

    /**
     * 权限 保存
     */
    public function save()
    {
        $functionId = I('post.id');
        $name = I('post.name');
        $categoryId = I('post.categoryId');
        $url = I('post.url');
        $isMenu = I('post.isMenu');
        $sort = I('post.sort');

        $data['name'] = $name;
        $data['url'] = $url;
        $data['isMenu'] = $isMenu;
        $data['sort'] = $sort;
        if(!empty($categoryId)){
            $data['categoryId'] = $categoryId;
        }

        $userGroupFunctionModel = new UserGroupFunctionModel();
        $functionEdit = $userGroupFunctionModel->where('id = '.$functionId)->save($data);

        if($functionEdit){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
}